package com.food.restaurantpro.observers;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.database.tables.DishCategoryTable;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;
import com.food.restaurantpro.objects.DishCategory;

public class DishCategoryObserver implements Observer {

	private DishCategoryObserver _me = this;
	private boolean hadRegisterObserver = false;
	private ArrayList<DishCategory> mDishCategoryList;
	private DishCategoryObserverInterface mDishCategoryInterface;

	public DishCategoryObserver(
			DishCategoryObserverInterface dishCategoryObserverInterface) {
		mDishCategoryInterface = dishCategoryObserverInterface;
	}

	/**
	 * register observer and get all dish category. DishCategory khong co api
	 * update nen moi lan vao app la get lai toan bo dishcategory
	 */
	public void registerObserver() {
		// call api get all dish category
		if (ManagingNetwork.isNetworkAvailable()) {
			// show progress
			hadRegisterObserver = true;
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().getAllDishCategory();

		} else {
			// neu ko co mang thi tra ve null de ko lam gi het
			// Neu khac null thi phai download image cua dish category
			mDishCategoryInterface.dishCategoryCallback(null,
					RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.NETWORK_ERROR));
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		if (data == null)
			return;
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case getAllDishCategory:
			if (result.getErrorCode() != null) {
				// show error mesage
				mDishCategoryInterface.dishCategoryCallback(null, result
						.getErrorCode().getErrorMessage());
			} else {
				mDishCategoryList = (ArrayList<DishCategory>) result.getData();
				if (mDishCategoryList != null) {
					DishCategoryTable.deleteAll();
					DishCategoryTable.insertListDishCategory(mDishCategoryList);
					//
					mDishCategoryInterface.dishCategoryCallback(
							mDishCategoryList, null);
				} else {
					mDishCategoryInterface.dishCategoryCallback(null,
							RestaurantApp.CURRENT_CONTEXT.getResources()
									.getString(R.string.SERVER_ERROR));
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve
		// thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong
		// "case method:"
		// khong dat code o ngoai nay
	}

}
